package com.mission.game.creategame;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.mission.game.R;
import com.mission.game.components.RemoveAdapter;
import com.mission.game.utils.Game;
import com.mission.game.utils.Mission;

public class MissionControlScreen extends Activity {

	protected static final int CREATE_MISSION = 5;
	protected static final int EDIT_MISSION = 6;
	protected static final int RESULT_EDIT = 4;
	private final String NO_MISSION_STRING = "Click \"Add New Mission\" to begin.";
	private Game _game;
	private ArrayList<String> _missionNames;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.mission_control_screen);

		Bundle extras = getIntent().getExtras();
		if (extras != null) {
			_game = (Game) extras.getSerializable("game");
		}
		// Set chosen missions list
		setCurrentMissions();
	}

	private void setCurrentMissions() {
		ListView missionsLv = (ListView) findViewById(R.id.chosen_missions_list);
		_missionNames = new ArrayList<String>(_game.getMissionNames());
		final HashMap<String, Mission> missions = _game.getMissions();
		if (_missionNames.size() == 0) 
			hideNext();
		else
			showNext();
		RemoveAdapter missionAdapter = new RemoveAdapter(this,
				R.layout.removable_list_item, _missionNames);
		missionsLv.setAdapter(missionAdapter);
		missionsLv.setTextFilterEnabled(true);
		missionsLv.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				String clickedMission = _missionNames.get(position);
				if (clickedMission.equals(NO_MISSION_STRING)) {
					_missionNames.remove(NO_MISSION_STRING);
					onAddMissionButtonClicked(view);
				} else {
					Intent i = new Intent(getApplicationContext(),
							CreateMissionScreen.class);
					i.putExtra("game", _game);
					i.putExtra("mission", missions.get(clickedMission));
					// Should we remove it? Prevents duplicate with name change.
					_game.removeMission(clickedMission);
					startActivityForResult(i, EDIT_MISSION);
				}
			}

		});
	}
	
	private void hideNext() {
		//		_missionNames.add(NO_MISSION_STRING);
		findViewById(R.id.missions_title).setVisibility(View.INVISIBLE);
		findViewById(R.id.create_game_next_button_2).setVisibility(View.INVISIBLE);
	}

	private void showNext() {
		findViewById(R.id.missions_title).setVisibility(View.VISIBLE);
		findViewById(R.id.create_game_next_button_2).setVisibility(View.VISIBLE);		
	}

	public void removeMissionItem(final int position) {
		final String clickedMission = _missionNames.get(position);
		new AlertDialog.Builder(this)
				.setIcon(android.R.drawable.ic_dialog_alert)
				.setTitle("Remove Item")
				.setMessage(
						"Are you sure you want to remove " + clickedMission
								+ " from the list?")
				.setPositiveButton("Yes",
						new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								_missionNames.remove(clickedMission);
								_game.removeMission(clickedMission);
								setCurrentMissions();
							}

						}).setNegativeButton("No", null).show();
	}

	public void onAddMissionButtonClicked(View v) {
		Intent i = new Intent(getApplicationContext(),
				CreateMissionScreen.class);
		i.putExtra("game", _game);
		startActivityForResult(i, CREATE_MISSION);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		switch (requestCode) {
		case CREATE_MISSION:
		case EDIT_MISSION:
			if (resultCode == RESULT_OK && data != null
					&& data.getExtras() != null) {
				Mission mission = (Mission) data.getExtras().get("mission");
				if (!mission.getName().isEmpty()) {
					_game.addMission(mission);
				}
			}
			break;
		default:
			break;
		}
	}

	@Override
	protected void onResume() {
		super.onResume();
		setCurrentMissions();
	}

	public void editGame(View v) {
		Intent i = new Intent(getApplicationContext(),
				CreateNewGameScreen.class);
		i.putExtra("game", _game);
		setResult(RESULT_EDIT, i);
		finish();
	}

	public void goToNextScreen(View v) {
		//TODO Change to "InviteScreen" when created.
		Intent i = new Intent();
		i.putExtra("game", _game);
		setResult(Activity.RESULT_OK, i);
		finish();
	}

}
